package com.jz.community.basecomm.net.retrofit;

import android.content.Context;

import com.jz.community.basecomm.utils.Preconditions;

/**
 * Created by Tony.Fan on 1/30/2016 13:07.
 */
public class RequestBuilder<T> {

    private Context context;
    private String requestUrl;
    private Class<T> requestApi;

    private boolean isNeedHandleErrorBySelf;  //表示是否需要自己来处理错误信息,而不是统一处理

    public RequestBuilder(Context context) {
        this.context = context;
    }

    public Context getContext() {
        return context;
    }

    /**
     * 配置请求的baseurl，不配置为默认
     *
     * @param url
     * @return
     */
    public RequestBuilder setRequestUrl(String url) {
        this.requestUrl = url;
        return this;
    }

    /**
     * 是否需要自己来处理错误信息
     *
     * @param isNeedHandleErrorBySelf
     */
    public RequestBuilder setIsNeedHandleErrorBySelf(boolean isNeedHandleErrorBySelf) {
        this.isNeedHandleErrorBySelf = isNeedHandleErrorBySelf;
        return this;
    }


    /**
     * 配置请求对应的接口class,不配置为默认
     *
     * @param requestApi
     * @return
     */
    public RequestBuilder setRequestApi(Class<T> requestApi) {
        this.requestApi = requestApi;
        return this;
    }

    public String getRequestUrl() {
        return requestUrl;
    }

    public Class<T> getRequestApi() {
        return requestApi;
    }


    public boolean getIsNeedHandleErrorBySelf() {
        return isNeedHandleErrorBySelf;
    }

    public RequestService build() {
        return new RequestService(this);
    }

    public <T> T create(final Class<T> service) {
        return create(null, service);
    }

    public <T> T create(String baseUrl, final Class<T> service) {
        if (Preconditions.isNullOrEmpty(baseUrl)) {
            return new RequestService<T>(this).createRequest(service);
        }
        return new RequestService<T>(this).createRequest(baseUrl, service);
    }
}
